<?php
	/*
	* Process Event Locked
	* Return all event and book
	* Unlock each records
	*/
	class EventlockedController extends AdminController{
		public function actionIndex(){
			$event_locked = $this->get_event_lock();
			$this->render('index',array('event_locked' => $event_locked));
		}
		public function actionViewbooked()
		{
			$booked_locked = Booked::model()->findAllByAttributes(
				array(),
				array(
					'condition' => 'lock_session <> 0 OR user_lock <> 0'
					)
				);
			echo $this->renderPartial('_views',array('booked_locked' => $booked_locked));
		}
		public function actionViewevent(){
			$event_locked = $this->get_event_lock();
			echo $this->renderPartial('_views',array('event_locked' => $event_locked));
		}
		protected function get_event_lock()
		{
			$result = Event::model()->with(array('translate','category'))->findAll(
				'(t.lock_session <> 0 OR t.user_lock <> 0) AND (t.status = :stt_active OR t.status = :stt_inactive)',
				array(
					':stt_active' => STATUS_ACTIVE,
					':stt_inactive' => STATUS_INACTIVE
					)
				);
			return $result;
		}
		public function actionUnlockevent()
		{
			if(isset($_POST['event_id'])){
				$event = Event::model()->findByPk($_POST['event_id']);
				$event->lock_session = 0;
				$event->user_lock = 0;
				$event->save();
				echo CJSON::encode(1);
				Yii::app()->end();
			}
			if(isset($_POST['booked_id'])){
				$booked = Booked::model()->findByPk($_POST['booked_id']);
				$booked->lock_session = 0;
				$booked->user_lock = 0;
				$booked->save();
				echo CJSON::encode(1);
				Yii::app()->end();
			}
		}
	}